<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%request.setCharacterEncoding("UTF-8"); %>
<%@ page import="zongheshiyan.StudentBean"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>查找个人信息</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");

%>

<%
	
		String driver="com.mysql.jdbc.Driver";		// 加载数据库驱动，注册到驱动管理器
		String url = "jdbc:mysql://localhost:3306/jsp";// 数据库连接字符串
		String username = "root";								// 数据库用户名
		String password = "root";           // 数据库密码
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url,username,password); 		// 创建Connection连接
		PreparedStatement sql = conn.prepareStatement("select * from student1 where name=?");	// 添加图书信息的SQL语句

		sql.setString(1, name);			// 对SQL语句中的第1个参数赋


		ResultSet row=sql.executeQuery();			// 执行更新操作，返回所影响的行数
		List<StudentBean> list = new ArrayList<>();	
		while(row.next()){ 											
			StudentBean student = new StudentBean();				
			student.setId(row.getInt("id"));				// 对id属性赋值
			student.setName(row.getString("name"));		// 对name属性赋值
			student.setSex(row.getString("sex"));		// 对price属性赋值
			student.setAge(row.getInt("age"));	// 对bookCount属性赋值
			list.add(student); 				
		}
		request.setAttribute("list", list); 
		sql.close();							// 关闭PreparedStatement，释放资源
		conn.close();							// 关闭Connection，释放资源
	

%>
<div width="98%" align="center">
		<h2>所有学生信息</h2>
	</div>
	<table width="98%" border="0" align="center" cellpadding="0"
		cellspacing="1" bgcolor="#666666">
		<tr>
			<th bgcolor="#FFFFFF">ID</th>
			<th bgcolor="#FFFFFF">姓名</th>
			<th bgcolor="#FFFFFF">性别</th>
			<th bgcolor="#FFFFFF">年龄</th>
		</tr>
		<%

		if (list == null || list.size() < 1) {
				out.print("<tr><td bgcolor='#FFFFFF' colspan='5'>没有任何学生信息！</td></tr>");
			} else {
				// 遍历图书集合中的数据
				for (StudentBean student : list) {
				%>
		<tr align="center">
			<td bgcolor="#FFFFFF" ><%=student.getId()%></td>
			<td bgcolor="#FFFFFF"><%=student.getName()%></td>
			<td bgcolor="#FFFFFF"><%=student.getSex()%></td>
			<td bgcolor="#FFFFFF"><%=student.getAge()%></td>
			<td bgcolor="#FFFFFF">
			</td>
		</tr>
		<%
			}
		}
		%>	
				
</table>
</body>
</html>